package com.lvy.hczn.transmit.controller;

import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Spu;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.transmit.domain.ProductSupplierGoods;
import com.lvy.hczn.transmit.domain.dto.cteck.ProductSellerGoods;
import com.lvy.hczn.transmit.service.ProductSupplierGoodsService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/api/dt/pro/")
@RestController
@Slf4j
public class ProductSupplierGoodsController extends BaseController {

    @Autowired
    private ProductSupplierGoodsService productSupplierGoodsService;

    /**
     * 商品分类同步列表
     *
     * @param info
     * @return
     */
    @ApiOperation(value = "商品同步", notes = "商品同步")
    @PostMapping("data")
    public AjaxResult syncProduct(HttpServletRequest request, @RequestBody ParamInfo info) {
        try {
            productSupplierGoodsService.syncProductBatch(request, info);
            return AjaxResult.success();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("syncProduct同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }

    /**
     * 商品分类同步列表
     *
     * @param info
     * @return
     */
    @ApiOperation(value = "商品批量同步", notes = "商品批量同步")
    @PostMapping("batch")
    public AjaxResult syncProductBatch(HttpServletRequest request, @RequestBody ParamInfo info) {
        try {
            productSupplierGoodsService.syncProductBatch(request, info);
            return AjaxResult.success();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("syncProductBatch同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }

    /**
     * 商品分类同步列表
     *
     * @return
     */
    @ApiOperation(value = "商品批量同步到仓库", notes = "商品批量同步到仓库")
    @PostMapping("syncProductSupplierToWarehouse")
    public AjaxResult syncProductSupplierToWarehouse(HttpServletRequest request) {
        try {
            List<Spu> list = productSupplierGoodsService.syncProductSupplierToWarehouse(request);
            return AjaxResult.success(list);
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("syncProductSupplierToWarehouse同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }
}
